import { Builder, By, WebDriver } from 'selenium-webdriver';
import { AndroidConfig } from '../../profile/android/android_config.js';
import { doQuery } from '../function/xhs.js';

// 1. 启动 Appium WebDriver
async function startDriver(): Promise<WebDriver> {
  const capabilities = {
    platformName: 'Android',
    browserName: '',  // 添加这行，空字符串表示原生应用
    'appium:automationName': AndroidConfig.device.automationName,
    'appium:deviceName': AndroidConfig.device.deviceName,
    'appium:appPackage': AndroidConfig.app.xhs.appPackage,
    'appium:appActivity': AndroidConfig.app.xhs.appActivity,
    'appium:noReset': true,
    'appium:unicodeKeyboard': true,
    'appium:resetKeyboard': true,
  };
  return await new Builder()
    .usingServer(`${AndroidConfig.appium.protocol}://${AndroidConfig.appium.host}:${AndroidConfig.appium.port}${AndroidConfig.appium.path}`)
    .withCapabilities(capabilities)
    .build();
}

// 测试启动百度应用
async function main() {
  const driver = await startDriver();
  console.log('Start XHS App.....');
  
  
  // do something 
  await doQuery(driver);

  // wait for the app to load
  await new Promise(resolve => setTimeout(resolve, 3000));
  
  // quit application
  await driver.quit();
  console.log('Research all Success......');
}

// 运行测试
main().catch(console.error);
